<?php include("../includes/header-admin.php") ?>
<title>LazySwim</title>
<link rel="stylesheet" href="store.css" type="text/css" />
</head>

<body>
<div id="page">
    <div id="page-top">
        <div id="page-bottom">
          <div id="header">
                <div id="header-info">
                    <h1>LAZYSWIM</h1>
                    <div class="description">Simplistic Swim System</div>
                </div>
				<!-- Header Goes Here -->
                <?php printHeader(); ?>
				<!-- Header Ends Here -->
                <div id="header-date">Today is <?php echo date('l, F jS, Y'); ?></div>
                <div id="header-cats">
				<!-- Welcome Message Starts Here -->
                    <ul>
					<?php include("../../includes/welcomeBar.php"); ?>
					</ul>
				<!-- Welcome Message Ends Here -->	
                </div>
            </div>
            <div id="main">
                <div id="sidebar">                 
                    <?php
					// include the corresponding navigation pages
                    include("../../includes/navigation.php");
					?>     
                </div>                     

            <!-- main body -->
			<div class="featured-post"> 
			
				<div class="post-title"><h2> <a href="index.php">Manage Store</a> - View Order</h2></div>
				<div class="post-entry">   

				<form name="viewOrdersForm" action="listOrders.php" method="post">
					<input type="submit" name="submit" value="View Orders">
				</form>
				<br><br>
				
				<?php
					$orderID = (int)$_GET['orderID'];                
					// update
					if( $_POST['update'] ) {
						$orderID = (int)$_POST['orderID'];   
						$status = $_POST['status'];                
						mysql_query("UPDATE orders SET status = '$status' WHERE orderID = '$orderID' ") or die( mysql_error());
						
						echo "<font color=\"blue\">Order Updated! </font><br><br>";
					}
					
					// fetch the order
					$sql = "SELECT * FROM orders WHERE orderID = '$orderID'";
					$order = mysql_query($sql) or die(mysql_error()); 
					$order = mysql_fetch_assoc($order);
					
					//fetch the ordered items
					$sql = "SELECT * FROM itemsOrdered WHERE orderID = '$orderID'";
					$items = mysql_query($sql) or die(mysql_error()); 
				?>
				
				<table cellpadding=2>
				<form name="updateOrder" action="viewOrder.php" method="post">
					<input type="hidden" name="orderID" value="<?php echo $order[orderID]; ?>">
					<tr><td align="left">Order Number:  </td><td align="left"> <?php echo "$order[orderID]"; ?></td></tr>
					<tr><td align="left">Time: </td><td align="left"> <?php echo "$order[orderTime]"; ?></td></tr>               
					<tr><td align="left">Status</td><td>
						<select name="status">
						<option value="ITEMS ORDERED" <?php if($order[status] == "ITEMS ORDERED"){ echo "selected"; } ?>>ITEMS ORDERED</option>
						<option value="PAID" <?php if($order[status] == "PAID"){ echo "selected"; } ?>>PAID</option>
						<option value="CANCELLED" <?php if($order[status] == "CANCELLED"){ echo "selected"; } ?>>CANCELLED</option>                  
						</select>
					</td></tr>
					<tr><td colspan=2><input type="submit" value="update" name="update"></td></tr>
				</form>
				</table>
				
				<table cellpadding=2 cellpadding=2 width="65%">
				<tr><th>Product Name</th><th>Price per unit</th><th>Quantity Ordered</th><th>Subtotal</th></tr>
				<?php
				$total = 0;
				
				$i = 0;
				while($row = mysql_fetch_assoc($items)) { 
					// this is just to make the table pretty with alternating tr colours
					if($i % 2) { 
						$class = 'row1'; 
					} 
					else { 
						$class = 'row2'; 
					} 
					$i++;
					
					//fetch the product info
					$sql = "SELECT * FROM products WHERE productID = '$row[productID]'";
					$product = mysql_query($sql) or die(mysql_error()); 
					$product = mysql_fetch_assoc($product);
					
					$total += $row[quantity] * $product[price];
					?>
					<tr class="<?php echo $class; ?>"> 
						<td align="center"><?php echo $product[name]; ?></td> 
						<td align="center">$<?php echo $product[price]; ?></td> 
						<td align="center"><?php echo $row[quantity]; ?></td>
						<td align="center">$<?php echo $row[quantity] * $product[price]; ?></td>
					</tr> 
					
				<?php    
				}
				?>
				<tr><th>Total Cost:</th><td colspan="2"></td><td align="center"><b>$<?php echo $total; ?></b><td></tr>
				</table>
				<!-- end of main -->
				</div>
			</div>	
				
            <div class="clear"></div>

            </div>
			<!-- Footer Goes Here -->
            <?php include("../../includes/footer.php"); ?>
			<!-- Footer Ends Here -->
        </div>
    </div>
</div>
</body>
</html>